package com.shishuo.cms.action.user;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qq.connect.QQConnectException;
import com.qq.connect.oauth.Oauth;
import com.shishuo.cms.auth.UserAuthPassport;
import com.shishuo.cms.constant.ExceptionConstant;
import com.shishuo.cms.constant.UserConstant;
import com.shishuo.cms.entity.CashEntity;
import com.shishuo.cms.entity.GoldEntity;
import com.shishuo.cms.entity.MyNetEntity;
import com.shishuo.cms.entity.UserCallBackGoods;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.UserFavGoods;
import com.shishuo.cms.entity.UserScanGoods;
import com.shishuo.cms.entity.UserWebEmail;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.util.AuthUtils;

@Controller
@RequestMapping("/user")
public class UserIndexAction extends UserBaseAction {

	@InitBinder("userEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userEntity.");
	}

	@RequestMapping(value = { "/login.htm" }, method = RequestMethod.GET)
	public String login(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		if (null != session
				&& null != session.getAttribute(UserConstant.SESSION_USER)) {
			response.sendRedirect("/user/index.htm");
		}
		return this.getTemplatePath() + "user/login";
	}
	@RequestMapping(value = { "/qq.htm" }, method = RequestMethod.GET)
	public void qq(ModelMap modelMap) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		try {
			response.sendRedirect(new Oauth().getAuthorizeURL(request));
		} catch (QQConnectException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value = { "/reg.htm" }, method = RequestMethod.GET)
	public String reg(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		if (null != session
				&& null != session.getAttribute(UserConstant.SESSION_USER)) {
			response.sendRedirect("/user/index.htm");
		}
		return this.getTemplatePath() + "user/reg";
	}

	@RequestMapping(value = { "/getPass.htm" }, method = RequestMethod.GET)
	public String getPass(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		if (null != session
				&& null != session.getAttribute(UserConstant.SESSION_USER)) {
			response.sendRedirect("/user/index.htm");
		}
		return this.getTemplatePath() + "user/getPass";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/logout.htm" }, method = RequestMethod.GET)
	public String logout(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		session.setAttribute(UserConstant.SESSION_USER, null);
		return this.getTemplatePath() + "user/login";
	}

	@RequestMapping(value = { "/noAuth.htm" }, method = RequestMethod.GET)
	public String noAuth(ModelMap modelMap) throws Exception {
		return this.getTemplatePath() + "user/noAuth";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/index.htm", "/" }, method = RequestMethod.GET)
	public String index(ModelMap modelMap) throws Exception {
		updateUserSession();
		return this.getTemplatePath() + "user/index";
	}

	@ResponseBody
	@RequestMapping(value = "/login.json", method = RequestMethod.POST)
	public JsonVo<String> loginJson(@ModelAttribute UserEntity userEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(userEntity.getUserName())) {
			json.getErrors().put("userName", "用户名不能为空");
		}
		if (StringUtils.isBlank(userEntity.getPassWord())) {
			json.getErrors().put("passWord", "密码不能为空");
		} else if (userEntity.getPassWord().length() < 6
				&& userEntity.getPassWord().length() > 30) {
			json.getErrors().put("passWord", "密码最少6个字符，最多30个字符");
		}
		if (validateJsonIsError(json))
			return json;
		userEntity.setPassWord(AuthUtils.getPassword(userEntity.getPassWord(),
				userEntity.getUserName()));
		UserEntity userEntityRet = userService
				.getByUserNameAndPassword(userEntity);
		HttpSession session = request.getSession();
		userEntityRet.setPassWord("");
		session.setAttribute(UserConstant.SESSION_USER, userEntityRet);
		json.setResult(true);
		json.setMsg("登录成功！");
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/reg.json", method = RequestMethod.POST)
	public JsonVo<String> regJson(@ModelAttribute UserEntity userEntity,
			@RequestParam(value = "imgcode") String captcha) throws Exception {
		String kaptcha = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isNotBlank(kaptcha)
				&& kaptcha.equalsIgnoreCase(captcha)) {
		} else {
			json.getErrors().put("imgcode", "验证码错误");
		}
		if (StringUtils.isBlank(userEntity.getUserName())) {
			json.getErrors().put("userName", "用户名不能为空");
		}
		if (StringUtils.isBlank(userEntity.getPassWord())) {
			json.getErrors().put("passWord", "密码不能为空");
		} else if (userEntity.getPassWord().length() < 6
				&& userEntity.getPassWord().length() > 30) {
			json.getErrors().put("passWord", "密码最少6个字符，最多30个字符");
		}
		if (validateJsonIsError(json))
			return json;
		userEntity.setPassWord(AuthUtils.getPassword(userEntity.getPassWord(),
				userEntity.getUserName()));
		userEntity.setRoleId(1);
		userService.add(userEntity);
		json.setResult(true);
		json.setMsg("注册成功！");
		return json;
	}

	@UserAuthPassport
	@ResponseBody
	@RequestMapping(value = "/searchCenterNameJson.json", method = RequestMethod.POST)
	public JsonVo<UserEntity> searchCenterNameJson(
			@RequestParam(value = "input_center_name") String input_center_name) {
		JsonVo<UserEntity> json = new JsonVo<UserEntity>();
		json.setResult(false);
		if (StringUtils.isNotBlank(input_center_name)
				&& input_center_name.equalsIgnoreCase(input_center_name)) {
		} else {
			json.getErrors().put("input_center_name", "报单中心不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		UserEntity retUserEntity = userService.getSingleUser(input_center_name);
		if (null != retUserEntity
				&& retUserEntity.getUserRoleEntity().getRoleName()
						.equals("报单中心")) {
			UserEntity newUserEntity = new UserEntity();
			newUserEntity.setBankCard(retUserEntity.getBankCard());
			newUserEntity.setBankName(retUserEntity.getBankName());
			newUserEntity.setUserRealName(retUserEntity.getUserRealName());
			json.setT(newUserEntity);
			json.setResult(true);
			json.setMsg("查询成功！");
			return json;
		} else {
			json.getErrors().put("input_center_name", "报单中心不存在");
		}
		json.setMsg("查询失败，报单中心不存在！");
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/ajaxUserName.json", method = RequestMethod.POST)
	public JsonVo<String> ajaxUserNameJson(@ModelAttribute UserEntity userEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(userEntity.getUserName())) {
			json.getErrors().put("userName", "用户名不能为空");
		}
		if (userService.existUser(userEntity.getUserName())) {
			json.getErrors().put("userName", "用户名已经存在");
		}
		if (validateJsonIsError(json))
			return json;
		json.setResult(true);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/captchaAjax.json", method = RequestMethod.POST)
	public JsonVo<String> captchaAjax(
			@RequestParam(value = "imgcode") String captcha) throws Exception {
		String kaptcha = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		JsonVo<String> json = new JsonVo<String>();
		// 校验验证码
		if (StringUtils.isNotBlank(kaptcha)
				&& kaptcha.equalsIgnoreCase(captcha)) {

		} else {
			json.getErrors().put("imgcode", "验证码错误");
		}
		if (validateJsonIsError(json))
			return json;
		json.setResult(true);
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/changePassword.htm" }, method = RequestMethod.GET)
	public String changePassword(ModelMap modelMap) throws Exception {
		return this.getTemplatePath() + "user/changePassword";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/changePasswordJson.json", method = RequestMethod.POST)
	public JsonVo<String> changePasswordJson(
			@RequestParam(value = "oldpass") String oldpass,
			@RequestParam(value = "password") String password) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验
		if (StringUtils.isBlank(password)) {
			json.getErrors().put("passWord", "密码不能为空");
		} else if (password.length() < 6 && password.length() > 30) {
			json.getErrors().put("passWord", "密码最少6个字符，最多30个字符");
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		userService.updateUserPassword(userEntity, password, oldpass);
		json.setResult(true);
		json.setMsg("修改成功！");
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/changeExchangePassword.htm" }, method = RequestMethod.GET)
	public String changeExchangePassword(ModelMap modelMap) throws Exception {
		return this.getTemplatePath() + "user/changeExchangePassword";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/changeExchangePasswordJson.json", method = RequestMethod.POST)
	public JsonVo<String> changeExchangePasswordJson(
			@RequestParam(value = "oldpass") String oldpass,
			@RequestParam(value = "chargePassWord") String chargePassWord)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验
		if (StringUtils.isBlank(chargePassWord)) {
			json.getErrors().put("passWord", "密码不能为空");
		} else if (chargePassWord.length() < 6 && chargePassWord.length() > 30) {
			json.getErrors().put("passWord", "密码最少6个字符，最多30个字符");
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		userService.updateUserExchangePassword(userEntity, chargePassWord,
				oldpass);
		json.setResult(true);
		json.setMsg("修改成功！");
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/getPass.json", method = RequestMethod.POST)
	public JsonVo<String> getPassJson(
			@RequestParam(value = "emailpass") String emailpass,
			@RequestParam(value = "imgcode") String imgcode) throws Exception {
		String kaptcha = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		JsonVo<String> json = new JsonVo<String>();
		// 校验验证码
		if (StringUtils.isNotBlank(kaptcha)
				&& kaptcha.equalsIgnoreCase(imgcode)) {

		} else {
			json.getErrors().put("imgcode", "验证码错误");
		}
		if (validateJsonIsError(json))
			return json;
		boolean ret = userService.sendMailForGetPassword(emailpass);
		if (true == ret) {
			json.setResult(true);
			json.setMsg("邮件发送成功，请点击邮箱内链接取回密码！");
		} else {
			json.getErrors().put("emailpass", "邮件发送失败，稍后重试！");
		}
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/userInfo.htm" }, method = RequestMethod.GET)
	public String userInfo(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		userEntity = userService.getById(userEntity.getId());
		modelMap.put("userEntity", userEntity);
		return this.getTemplatePath() + "user/userInfo";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/userInfo.json", method = RequestMethod.POST)
	public JsonVo<String> userInfoJson(@ModelAttribute UserEntity userEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验

		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntitySession = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		UserEntity userEntityDb = userService
				.getById(userEntitySession.getId());
		userService.updateUserInfo(userEntityDb, userEntity);
		json.setResult(true);
		json.setMsg("修改成功！");
		updateUserSession();
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/pay.htm" }, method = RequestMethod.GET)
	public String pay(ModelMap modelMap) throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		modelMap.put("centerUserName", userEntity.getCenterUserName());
		return this.getTemplatePath() + "user/pay";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/payJson.json", method = RequestMethod.POST)
	public JsonVo<String> payJson(
			@RequestParam(value = "input_money") double inputMoney,
			@RequestParam(value = "input_center_name") String input_center_name,
			@RequestParam(value = "cashMsg") String cashMsg) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验
		if (inputMoney < 1) {
			json.getErrors().put("input_money", "充值金额不能小于1");
		}
		if (StringUtils.isNotBlank(cashMsg)) {

		} else {
			json.getErrors().put("cashMsg", "转账流水号不能为空");
		}
		long passUserId = 0;
		UserEntity retUserEntity = userService.getSingleUser(input_center_name);
		if (null != retUserEntity
				&& retUserEntity.getUserRoleEntity().getRoleName()
						.equals("报单中心")) {
			passUserId = retUserEntity.getId();
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		cashService.inputMoney(userEntity.getId(), inputMoney, passUserId,
				cashMsg);
		json.setResult(true);
		json.setMsg("充值提交成功，待审核！");
		updateUserSession();
		return json;
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/exchangeJson.json", method = RequestMethod.POST)
	public JsonVo<String> exchangeJson(
			@RequestParam(value = "input_gold") double inputGold)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验
		if (inputGold < 100) {
			json.getErrors().put("input_gold", "兑换数量不能小于100");
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		goldService.exchangeGold(userEntity.getId(), inputGold);
		json.setResult(true);
		json.setMsg("兑换成功！");
		updateUserSession();
		return json;
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/exchangeMoneyToGoldJson.json", method = RequestMethod.POST)
	public JsonVo<String> exchangeMoneyToGoldJson(
			@RequestParam(value = "input_money") double inputMoney)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		// 校验
		if (inputMoney < 100) {
			json.getErrors().put("input_gold", "兑换金额不能小于100");
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		goldService.exchangeMoney(userEntity.getId(), inputMoney);
		json.setResult(true);
		json.setMsg("兑换成功！");
		updateUserSession();
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/money.htm" }, method = RequestMethod.GET)
	public String money(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<CashEntity> pageVo = cashService.getAllListPage(p,
				userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/money";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/exchange.htm" }, method = RequestMethod.GET)
	public String exchange(ModelMap modelMap) throws Exception {
		return this.getTemplatePath() + "user/exchange";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/gold.htm" }, method = RequestMethod.GET)
	public String gold(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<GoldEntity> pageVo = goldService.getAllListPage(p,
				userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/gold";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myNet.htm" }, method = RequestMethod.GET)
	public String myNet(ModelMap modelMap,
			@RequestParam(value = "userName", defaultValue = "") String userName)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		if (!"".equals(userName)) {
			userEntity = userService.getSingleUser(userName);
		}
		modelMap.put("userEntity", userEntity);
		if (null != userEntity) {
			List<MyNetEntity> listMyNetEntity1 = myNetService
					.getMyNetEntityByParentUserName(userEntity.getUserName());
			if (null != listMyNetEntity1) {
				modelMap.put("net0", listMyNetEntity1);
				for (int i = 0; i < listMyNetEntity1.size(); i++) {
					List<MyNetEntity> listMyNetEntity2 = myNetService
							.getMyNetEntityByParentUserName(listMyNetEntity1
									.get(i).getUserEntity().getUserName());
					modelMap.put("net1" + i, listMyNetEntity2);
					if (null != listMyNetEntity2) {
						for (int j = 0; j < listMyNetEntity2.size(); j++) {
							List<MyNetEntity> listMyNetEntity3 = myNetService
									.getMyNetEntityByParentUserName(listMyNetEntity2
											.get(j).getUserEntity()
											.getUserName());
							modelMap.put("net2" + i + "" + j, listMyNetEntity3);
						}
					}
				}
			}
		}

		return this.getTemplatePath() + "user/myNet";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myReg.htm" }, method = RequestMethod.GET)
	public String myReg(ModelMap modelMap) throws Exception {
		return this.getTemplatePath() + "user/myReg";
	}

	@UserAuthPassport
	@ResponseBody
	@RequestMapping(value = "/myReg.json", method = RequestMethod.POST)
	public JsonVo<String> myRegJson(@ModelAttribute UserEntity userEntity,
			@RequestParam(value = "imgcode") String captcha,
			@RequestParam(value = "referUserName") String referUserName,
			@RequestParam(value = "centerUserName") String centerUserName)
			throws Exception {
		String kaptcha = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isNotBlank(kaptcha)
				&& kaptcha.equalsIgnoreCase(captcha)) {
		} else {
			json.getErrors().put("imgcode", "验证码错误");
		}
		if (StringUtils.isBlank(userEntity.getUserName())) {
			json.getErrors().put("userName", "用户名不能为空");
		}
		if (StringUtils.isBlank(referUserName)) {
			json.getErrors().put("referUserName", "推荐人不能为空");
		}
		if (StringUtils.isBlank(centerUserName)) {
			json.getErrors().put("centerUserName", "报单中心不能为空");
		}
		if (StringUtils.isBlank(userEntity.getPassWord())) {
			json.getErrors().put("passWord", "密码不能为空");
		} else if (userEntity.getPassWord().length() < 6
				&& userEntity.getPassWord().length() > 30) {
			json.getErrors().put("passWord", "密码最少6个字符，最多30个字符");
		}
		if (validateJsonIsError(json))
			return json;
		userEntity.setPassWord(AuthUtils.getPassword(userEntity.getPassWord(),
				userEntity.getUserName()));
		userService.innerReg(userEntity, referUserName, centerUserName);
		json.setResult(true);
		json.setMsg("注册成功！");
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myRefer.htm" }, method = RequestMethod.GET)
	public String myRefer(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserEntity> pageVo = userService.getMyReferByUserName(
				userEntity.getUserName(), p);
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myRefer";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myActiveUser.htm" }, method = RequestMethod.GET)
	public String myActiveUser(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserEntity> pageVo = userService.getMyActiveUserByUserName(
				userEntity.getUserName(), p);
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myActiveUser";
	}

	/**
	 * 打开用户激活页面
	 * 
	 * @param modelMap
	 * @param parentUserName挂接上级用户名
	 * @return
	 * @throws Exception
	 */
	@UserAuthPassport
	@RequestMapping(value = { "/activeUser.htm" }, method = RequestMethod.GET)
	public String activeUser(ModelMap modelMap,
			@RequestParam(value = "userId", defaultValue = "0") long userId)
			throws Exception {
		UserEntity activeUserEntity = userService.getById(userId);
		modelMap.put("activeUserEntity", activeUserEntity);
		return this.getTemplatePath() + "user/activeUser";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/activeUserJson.json", method = RequestMethod.POST)
	public JsonVo<String> activeUserJson(
			@RequestParam(value = "userId", defaultValue = "0") long userId,
			@RequestParam(value = "parentUserName", defaultValue = "0") String parentUserName)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (StringUtils.isBlank(parentUserName)) {
			json.getErrors().put("parentUserName", "挂接上级不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		userService.activeUser(userId, userEntity, parentUserName);
		json.setResult(true);
		json.setMsg("激活成功！");
		updateUserSession();
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/withdraw.htm" }, method = RequestMethod.GET)
	public String withdraw(ModelMap modelMap) throws Exception {
		// 获取当前登录用户的报单中心
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		modelMap.put("centerUserName", userEntity.getCenterUserName());
		return this.getTemplatePath() + "user/withdraw";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/withdrawJson.json", method = RequestMethod.POST)
	public JsonVo<String> withdrawJson(
			@RequestParam(value = "output_money", defaultValue = "0") double outputMoney,
			@RequestParam(value = "input_center_name") String input_center_name,
			@RequestParam(value = "cashMsg", defaultValue = "") String cashMsg)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (outputMoney < 100) {
			json.getErrors().put("output_money", "提现金额不能小于100");
		}
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		if (userEntity.getBankCard() == null
				|| userEntity.getUserRealName() == null) {
			json.getErrors().put("userRealName", "请先设置提现银行及其提款账号！");
		}
		long passUserId = 0;
		UserEntity retUserEntity = userService.getSingleUser(input_center_name);
		if (null != retUserEntity
				&& retUserEntity.getUserRoleEntity().getRoleName()
						.equals("报单中心")) {
			passUserId = retUserEntity.getId();
		} else {
			throw new Exception(
					ExceptionConstant.WITHDRAW_ERROR_CENTER_USER_IS_NOT_EXIST);
		}
		if (validateJsonIsError(json))
			return json;
		cashService.withdraw(outputMoney, userEntity.getId(), passUserId,
				cashMsg);
		json.setResult(true);
		json.setMsg("提现成功，待审核！");
		updateUserSession();
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/cash.htm" }, method = RequestMethod.GET)
	public String cash(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<CashEntity> pageVo = cashService.getAllListPageByPassUserId(p,
				userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/cash";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/processCash.htm" }, method = RequestMethod.GET)
	public String processCash(ModelMap modelMap,
			@RequestParam(value = "id", defaultValue = "0") long id)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		CashEntity cashEntity = cashService.getById(id);
		if (null != cashEntity) {
			if (userEntity.getId() == cashEntity.getPassUserId()) {
				modelMap.put("cashEntity", cashEntity);
			} else {
				throw new Exception(
						ExceptionConstant.CASH_PROCESS_ERROR_USER_IS_NOT_PERMITED);
			}
		}
		return this.getTemplatePath() + "user/processCash";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/processCash.json", method = RequestMethod.POST)
	public JsonVo<String> processCashJson(@RequestParam(value = "id") long id,
			@RequestParam(value = "passMsg") String passMsg) throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		JsonVo<String> json = new JsonVo<String>();
		cashService.processCashByUser(userEntity.getId(), id, passMsg);
		updateUserSession();
		json.setResult(true);
		json.setMsg("处理成功！");
		return json;
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/addGoodsFavJson.json", method = RequestMethod.GET)
	public JsonVo<String> addGoodsFavJson(
			@RequestParam(value = "goodsId") long goodsId) throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		JsonVo<String> json = new JsonVo<String>();
		UserFavGoods userFavGoods = new UserFavGoods();
		userFavGoods.setUserId(userEntity.getId());
		userFavGoods.setGoodsId(goodsId);
		userFavGoodsService.saveUserFavGoods(userFavGoods);
		json.setResult(true);
		json.setMsg("收藏成功！");
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myGoodsFav.htm" }, method = RequestMethod.GET)
	public String myGoodsFav(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserFavGoods> pageVo = userFavGoodsService.getAllListPage(p,
				userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myGoodsFav";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myGoodsScan.htm" }, method = RequestMethod.GET)
	public String myGoodsScan(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserScanGoods> pageVo = userScanGoodsService.getAllListPage(p,
				userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myGoodsScan";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myGoodsCallBack.htm" }, method = RequestMethod.GET)
	public String myGoodsCallBack(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int p)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserCallBackGoods> pageVo = userCallBackGoodsService
				.getAllListPage(p, userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myGoodsCallBack";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/goodsCallBackOrder.htm" }, method = RequestMethod.GET)
	public String goodsCallBackOrder(ModelMap modelMap,
			@RequestParam(value = "goodsId", defaultValue = "0") Long goodsId)
			throws Exception {
		modelMap.put("id", goodsId);
		modelMap.put("p", 0);
		return this.getTemplatePath() + "user/addGoodsCallBackOrder";
	}

	@ResponseBody
	@UserAuthPassport
	@RequestMapping(value = "/goodsCallBackOrderJson.json", method = RequestMethod.POST)
	public JsonVo<String> goodsCallBackOrderJson(
			@RequestParam(value = "goodsId") long goodsId,
			@RequestParam(value = "goodsOrderNum") String goodsOrderNum)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		JsonVo<String> json = new JsonVo<String>();
		UserCallBackGoods userCallBackGoods = new UserCallBackGoods();
		userCallBackGoods.setUserId(userEntity.getId());
		userCallBackGoods.setGoodsId(goodsId);
		userCallBackGoods.setGoodsOrderNum(goodsOrderNum);
		userCallBackGoodsService.saveUserCallBackGoods(userCallBackGoods);
		json.setResult(true);
		json.setMsg("提交成功！");
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/getLoginUser.json", method = RequestMethod.GET)
	public JsonVo<UserEntity> getLoginUserJson() throws Exception {
		JsonVo<UserEntity> json = new JsonVo<UserEntity>();
		HttpSession session = request.getSession();
		if (null != session) {
			UserEntity userEntity = (UserEntity) session
					.getAttribute(UserConstant.SESSION_USER);
			if (null != userEntity) {
				json.setT(userEntity);
				json.setResult(true);
				json.setMsg("已登录！");
				return json;
			}
		}
		json.setResult(false);
		json.setMsg("未登录！");
		return json;
	}

	@UserAuthPassport
	@RequestMapping(value = { "/myWebEmail.htm" }, method = RequestMethod.GET)
	public String myWebEmail(ModelMap modelMap,
			@RequestParam(value = "p", defaultValue = "0") int pageNum)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		PageVo<UserWebEmail> pageVo = userWebEmailService.getAllListPage(
				pageNum, userEntity.getId());
		modelMap.put("pageVo", pageVo);
		return this.getTemplatePath() + "user/myWebEmail";
	}

	@UserAuthPassport
	@RequestMapping(value = { "/webEmailItem.htm" }, method = RequestMethod.GET)
	public String webEmailItem(ModelMap modelMap,
			@RequestParam(value = "id", defaultValue = "0") long id)
			throws Exception {
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session
				.getAttribute(UserConstant.SESSION_USER);
		UserWebEmail userWebEmail = userWebEmailService.getById(id);
		if (userEntity.getId() != userWebEmail.getAcceptUserId()) {
			throw new Exception("错误，无权限阅读！");
		} else {
			userWebEmailService.readEmail(userWebEmail);
		}
		modelMap.put("userWebEmail", userWebEmail);
		return this.getTemplatePath() + "user/webEmailItem";
	}

}
